
layui.use(['form', 'layedit', 'laydate',"table",'upload'], function() {
    var $ = layui.jquery
        , upload = layui.upload;
    var table = layui.table;
    var form = layui.form
        , layer = layui.layer
        , layedit = layui.layedit
        , laydate = layui.laydate;

    //自定义验证规则
    form.verify({

        search: function (value) {
            if(!new RegExp("^[+]{0,1}(\\d+)$").test(value)&&value != null&&value != ""){
                return '热搜词排序为正整数';
            }
            if (value >999999){
                return '热搜词排序数过大';

            }
        }

    });
    
    $('#add').click(function () {
        $('#bgm')[0].reset()
        $('#musicId').val("");
        $('#bgmAudio').attr("src","");
        var  width=($(window).width()*0.6);
        var height=($(window).height()*0.9);
        layer.open({
            type: 1,
            closeBtn: 0,
            area: [width + 'px', height + 'px'],//大小
            content: $("#music"),
            skin: "layui-layer-molv",
            title: "音乐新增"
            ,btnAlign: 'c'
            ,btn: ['保存', '取消']
            ,yes: function(index, layero){
                layero.find('form').find('button[lay-submit]').click();
                return false
            }
            ,btn2: function(){
                layer.closeAll();
                location.href=location.href;
            }
        })

    })

    //监听工具条
    table.on('tool(demo)', function(obj){
        var data = obj.data;
        if(obj.event === 'detail'){
            $.ajax({
                url:"/musicController/openBgm",
                type:"GET",
                data:{musicId:data.musicId},
                success:function(data,status,xhr){
                    if(xhr.status == 200){

                        layer.open({
                            type: 1,
                            title: false,
                            area: ['630px', '360px'],
                            shade: 0.8,
                            closeBtn: 0,
                            shadeClose: true,
                            content:data.url
                        });
                    }
                }
            })
        } else if(obj.event === 'del'){
            layer.confirm('确定删除吗?', function(index){
                $.ajax({
                    url:"/topicController/delById",
                    type:"POST",
                    data:{id:data.id},
                    dataType:"json",
                    success:function(data,status,xhr){
                        if (xhr.code == -1){
                            top.location.href="/pageController/login"

                        }
                        if(xhr.status == 204){
                            layer.alert('删除成功', {icon: 1}, function(index){
                                layer.closeAll("page");
                                layer.close(index);
                                var curr = sessionStorage.getItem("curr");
                                table.reload('testReload',{page:{curr:curr}});
                            });
                        }else{
                            layer.alert('操作失败', {icon: 5});
                        }
                    }
                })
                layer.close(index);

            });
        } else if(obj.event === 'edit'){
            var musicId=  data.musicId
            $.ajax({
                url:"/musicController/openBgm?musicId="+musicId,
                type:"POST",
                dataType:"json",
                success:function (data,status,xhr) {
                    $('#bgmAudio').attr("src",data.url);

                }
            })
            $('#bgm')[0].reset()
            $('#musicId').val(data.id);
            $("input[name='title']").val(data.title )
            $("input[name='musicId']").val(data.musicId )
            $("input[name='artist']").val(data.artist)
            $("input[name='sortIndex']").val(data.sortIndex)
            form.render();
            var  width=($(window).width()*0.6);
            var height=($(window).height()*0.9);
            layer.open({
                type: 1,
                closeBtn: 0,
                area: [width + 'px', height + 'px'],//大小
                content: $("#music"),
                skin: "layui-layer-molv",
                title:  "编辑"
                ,btnAlign: 'c'
                ,btn: ['保存', '取消']
                ,yes: function(index, layero){
                    layero.find('form').find('button[lay-submit]').click();
                    return false
                }
                ,btn2: function(){
                    layer.closeAll();
                    location.href=location.href;
                }
            })
        }
    });





    //提交新增与更新
    form.on('submit(addAndUpdate)', function(data){
        var musicId =$('input[name="musicId"]').val()
        if (musicId == null || musicId ==""){
            layer.alert('请上传bgm', {icon: 5});
            return false;
        }
        $.ajax({
            url:"/musicController/addAndUpdate",
            type:"POST",
            data:data.field,
            dataType:"json",
            success:function(data,status,xhr){
                if(xhr.code == -1){
                    top.location.href="/pageController/login"
                }
                if(xhr.status == 201){
                    layer.alert('新增成功', {icon: 1}, function(index){
                        layer.closeAll("page");
                        layer.close(index);
                        var curr = sessionStorage.getItem("curr");
                        table.reload('testReload',{page:{curr:curr}});
                    });
                }
                if(xhr.status == 204){
                    layer.alert('更新成功', {icon: 1}, function(index){
                        layer.closeAll("page");
                        layer.close(index);
                        var curr = sessionStorage.getItem("curr");
                        table.reload('testReload',{page:{curr:curr}});
                    });
                }

            }
            ,error:function () {
                layer.alert('系统错误', {icon: 5});

            }
        })
        return false;
    });
    //页面加载完成
    $(function () {

    })

    //bgm上传
    var uploadInst = upload.render({
        elem: '#uploadBgm'
        ,url: '/musicController/upload'
        ,accept: 'audio' //音频
        ,exts:"mp3"
        ,before: function(obj){
            layer.load();
        }
        ,done: function(data,status,xhr){
            layer.closeAll('loading');
            layer.alert('上传成功', {icon: 1}, function(index){
                layer.close(index);
                $('#bgmAudio').attr("src",data.url);
                $('input[name="musicId"]').val(data.aliyunId);;
                $("#audio").show()
            });
        }
        ,error:function () {
            layer.closeAll('loading');

        }

    });

});



